package com.yeyks.commonReference.append.mattress.wechat.service;

import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.commonReference.append.mattress.wechat.config.MattressWxConfigUtil;
import com.yeyks.commonReference.append.mattress.wechat.config.MattressWxProperties;
import com.yeyks.commonReference.append.mattress.wechat.param.MattressWechatPayServiceGenerateWeChatOrdersParam;
import com.yeyks.commonReference.append.mattress.wechat.param.MattressWechatPayServiceRefundParam;
import com.yeyks.commonReference.service.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@ConditionalOnBean({MattressWxProperties.class})
public class MattressWechatPayService {

    @Autowired
    private MattressWxConfigUtil mattressWxConfigUtil;
    @Autowired
    private ConfigService configService;

    private WxPayService updateWxPayService(WxPayService wxPayService){
        WxPayConfig wxPayConfig = wxPayService.getConfig();
        if("1".equals(configService.getValue("mattress.mchid.enable")))
        {
            wxPayConfig.setMchId(configService.getValue("mattress.mchid"));
            wxPayConfig.setMchKey(configService.getValue("mattress.mchkey"));
            wxPayConfig.setKeyPath(configService.getValue("mattress.mchkeypath"));
            wxPayService.setConfig(wxPayConfig);
        }
        return wxPayService;
    }

    //生成微信支付订单
    public Map<String, Object> generateWeChatOrders(MattressWechatPayServiceGenerateWeChatOrdersParam param) {
        try {
            WxPayService wxPayService = mattressWxConfigUtil.getWxPayService(param.getPayType());
            wxPayService = updateWxPayService(wxPayService);
            //生成预支付订单
            WxPayUnifiedOrderRequest prepayInfo = WxPayUnifiedOrderRequest.newBuilder()
                    .openid(param.getOpenId())
                    .outTradeNo(param.getOrderNo())
                    .totalFee(param.getTotalFee())
                    .body(param.getBody())
                    .tradeType(param.getTradeType())
                    .spbillCreateIp(param.getCreateIp())
                    .notifyUrl(param.getNotifyUrl())
                    .attach(param.getAttach())
                    .build();
            WxPayMpOrderResult unifiedOrder = null;
            try {
                unifiedOrder = wxPayService.createOrder(prepayInfo);
            } catch (WxPayException e) {
                e.printStackTrace();
            }
//            log.info("微信订单退款返回信息result = {}", unifiedOrder);
            Map<String, Object> map = ObjectConvertUtil.objectToMap(unifiedOrder);
            if (map == null) {
                return new HashMap<>();
            }
            map.put("package", map.get("packageValue"));
            map.remove("packageValue");
            return map;
        } catch (Exception e) {
//            log.error("转换支付map失败{}", e.getLocalizedMessage());
            e.printStackTrace();
        }
        return new HashMap<>();
    }

    //退款
    public WxPayRefundResult refund(MattressWechatPayServiceRefundParam param) {
        String orderNo = param.getOrderNo();
        try {
            WxPayService wxPayService = mattressWxConfigUtil.getWxPayService(param.getPayType());
            wxPayService = updateWxPayService(wxPayService);
            //生成预支付订单
            WxPayRefundRequest refundRequest = WxPayRefundRequest.newBuilder()
                    .outTradeNo(orderNo)
                    .outRefundNo(orderNo + "Refund")
                    .totalFee(param.getTotalFee())
                    .refundFee(param.getRefundFee())
                    .build();
            WxPayRefundResult refund = wxPayService.refund(refundRequest);
            return refund;
        } catch (Exception e) {
            return null;
        }
    }

}
